﻿using System;
using System.Linq;
using System.Web.DomainServices;
using System.Web.DomainServices.Providers;
using System.Web.Ria;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.Web.Services
{
    [EnableClientAccess]
    [RequiresAuthentication]
    [RequiresRole("Administrator")]
    public class ProjectService : LinqToSqlDomainService<ProjectDBDataContext>
    {
        #region Projects
        public IQueryable<Project> GetProjects()
        {
            return this.DataContext.Projects;
        }

        public Project GetProjectByID(int projectID)
        {
            return this.DataContext.Projects.SingleOrDefault(p => p.ProjectID == projectID);
        }

        public void InsertProject(Project project)
        {
            project.CreationDate = DateTime.Now;
            this.DataContext.Projects.InsertOnSubmit(project);
        }

        public void UpdateProject(Project currentProject)
        {
            this.DataContext.Projects.Attach(currentProject, this.ChangeSet.GetOriginal(currentProject));
        }

        public void DeleteProject(Project project)
        {
            this.DataContext.Projects.Attach(project);
            this.DataContext.Projects.DeleteOnSubmit(project);
        }
        #endregion Projects

        #region Project Categories
        public IQueryable<ProjectCategory> GetProjectCategories()
        {
            return this.DataContext.ProjectCategories;
        }

        public IQueryable<ProjectCategory> GetProjectCategoriesForProject(int projectID)
        {
            return this.DataContext.ProjectCategories.Where(pc => pc.ProjectID == projectID);
        }

        public void InsertProjectCategory(ProjectCategory projectCategory)
        {
            if (projectCategory.Duration == null)
                projectCategory.Duration = 0;

            this.DataContext.ProjectCategories.InsertOnSubmit(projectCategory);
        }

        public void UpdateProjectCategory(ProjectCategory currentProjectCategory)
        {
            if (currentProjectCategory.Duration == null)
                currentProjectCategory.Duration = 0;

            this.DataContext.ProjectCategories.Attach(currentProjectCategory, this.ChangeSet.GetOriginal(currentProjectCategory));
        }

        public void DeleteProjectCategory(ProjectCategory projectCategory)
        {
            this.DataContext.ProjectCategories.Attach(projectCategory);
            this.DataContext.ProjectCategories.DeleteOnSubmit(projectCategory);
        }
        #endregion Project Categories

        #region Project Members
        public IQueryable<ProjectMember> GetProjectMembers()
        {
            return this.DataContext.ProjectMembers;
        }

        public IQueryable<ProjectMember> GetProjectMembersForUser(int userID)
        {
            return this.DataContext.ProjectMembers.Where(pm => pm.UserID == userID);
        }

        public IQueryable<ProjectMember> GetProjectMembersForProject(int projectID)
        {
            return this.DataContext.ProjectMembers.Where(pm => pm.ProjectID == projectID);
        }

        public void InsertProjectMember(ProjectMember projectMember)
        {
            this.DataContext.ProjectMembers.InsertOnSubmit(projectMember);
        }

        public void UpdateProjectMember(ProjectMember currentProjectMember)
        {
            this.DataContext.ProjectMembers.Attach(currentProjectMember, this.ChangeSet.GetOriginal(currentProjectMember));
        }

        public void DeleteProjectMember(ProjectMember projectMember)
        {
            this.DataContext.ProjectMembers.Attach(projectMember);
            this.DataContext.ProjectMembers.DeleteOnSubmit(projectMember);
        }
        #endregion Project Members
    }
}


